package com.whfc.common.util;

import com.whfc.common.enums.PhoneType;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * @Description: 正则表达式匹配
 * @Author: qzexing
 * @Version: 1.0
 * @Date: 2020/1/8 16:28
 */
public class RegexUtil {

    /**
     * 大陆手机号
     */
    //private final static Pattern MOBILE = Pattern.compile("^[1][3-9]\\d{9}$|^([6|9])\\d{7}$|^[0][9]\\d{8}$|^[6]([8|6])\\d{5}$");
    private final static Pattern MOBILE = Pattern.compile("^(\\+?86)?1\\d{10}$");

    /**
     * 港澳台地区电话
     */
    private final static Pattern HMT_MOBILE = Pattern.compile("^(((\\+)?|(00))?853)?([6|9])\\d{7}$|^[0][9]\\d{8}$|^[6]([8|6])\\d{5}$");

    /**
     * 身份证号
     */
    private final static Pattern ID_CARD = Pattern.compile("[1-9]\\d{5}[1-2]\\d{3}((0\\d)|(1[0-2]))(([012]\\d)|3[0-1])\\d{3}(\\d|X|x)");

    /**
     * IP
     */
    private final static Pattern IP = Pattern.compile("((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}");

    /**
     * 车牌号(兼容新能源)
     */
    private final static Pattern PLATE = Pattern.compile(
            "^(([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼][A-Z](([0-9]{5}[ABCDEFGHJK])|([ABCDEFGHJK]([A-HJ-NP-Z0-9])[0-9]{4})))|" +
                    "([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼][A-Z][A-HJ-NP-Z0-9]{4}[A-HJ-NP-Z0-9]))$");

    /**
     * 整数
     */
    private final static Pattern INTEGER = Pattern.compile("^[-+]?[\\d]*$");

    /**
     * RTMP流
     */
    private static final Pattern RTMP = Pattern.compile("^rtmp://.+");

    /**
     * HLS流
     */
    private static final Pattern HLS = Pattern.compile("^(http|https).+(\\.m3u8).*");

    /**
     * MP4
     */
    private static final Pattern MP4 = Pattern.compile("^(http|https).+\\.mp4$");

    /**
     * 定时任务表达式
     */
    private static final Pattern CRON = Pattern.compile("^\\s*($|#|\\w+\\s*=|(\\?|\\*|(?:[0-5]?\\d)(?:(?:-|\\/|\\,)" +
            "(?:[0-5]?\\d))?(?:,(?:[0-5]?\\d)(?:(?:-|\\/|\\,)(?:[0-5]?\\d))?)*)\\s+(\\?|\\*|(?:[0-5]?\\d)(?:(?:-|\\/|\\,)" +
            "(?:[0-5]?\\d))?(?:,(?:[0-5]?\\d)(?:(?:-|\\/|\\,)(?:[0-5]?\\d))?)*)\\s+(\\?|\\*|(?:[01]?\\d|2[0-3])(?:(?:-|\\/|\\,)" +
            "(?:[01]?\\d|2[0-3]))?(?:,(?:[01]?\\d|2[0-3])(?:(?:-|\\/|\\,)(?:[01]?\\d|2[0-3]))?)*)\\s+(\\?|\\*|(?:0?[1-9]|[12]\\d|3[01])" +
            "(?:(?:-|\\/|\\,)(?:0?[1-9]|[12]\\d|3[01]))?(?:,(?:0?[1-9]|[12]\\d|3[01])(?:(?:-|\\/|\\,)(?:0?[1-9]|[12]\\d|3[01]))?)*)" +
            "\\s+(\\?|\\*|(?:[1-9]|1[012])(?:(?:-|\\/|\\,)(?:[1-9]|1[012]))?(?:L|W)?(?:,(?:[1-9]|1[012])(?:(?:-|\\/|\\,)" +
            "(?:[1-9]|1[012]))?(?:L|W)?)*|\\?|\\*|(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)(?:(?:-)" +
            "(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?(?:,(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC)" +
            "(?:(?:-)(?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC))?)*)\\s+(\\?|\\*|(?:[0-6])(?:(?:-|\\/|\\,|#)" +
            "(?:[0-6]))?(?:L)?(?:,(?:[0-6])(?:(?:-|\\/|\\,|#)(?:[0-6]))?(?:L)?)*|\\?|\\*|(?:MON|TUE|WED|THU|FRI|SAT|SUN)" +
            "(?:(?:-)(?:MON|TUE|WED|THU|FRI|SAT|SUN))?(?:,(?:MON|TUE|WED|THU|FRI|SAT|SUN)(?:(?:-)(?:MON|TUE|WED|THU|FRI|SAT|SUN))?)*)" +
            "(|\\s)+(\\?|\\*|(?:|\\d{4})(?:(?:-|\\/|\\,)(?:|\\d{4}))?(?:,(?:|\\d{4})(?:(?:-|\\/|\\,)(?:|\\d{4}))?)*))$");


    private static final Pattern NET_URL = Pattern.compile("^(http|https)://[\\w\\-_]+(\\.[\\w\\-_]+)+([\\w\\-.,@?^=%&:/~+#]*[\\w\\-@?^=%&/~+#])?$");


    /**
     * 验证是否是整数
     *
     * @param value 值
     * @return 是否是整数
     */
    public static boolean isInteger(CharSequence value) {
        if (value == null) {
            return false;
        }
        return INTEGER.matcher(value).matches();
    }


    /**
     * 验证是否为手机号码
     *
     * @param value 值
     * @return 是否为手机号码
     */
    public static boolean isMobile(CharSequence value) {
        if (value == null) {
            return false;
        }
        return MOBILE.matcher(value).matches();
    }

    /**
     * 验证是否为港澳台手机号码
     *
     * @param value 值
     * @return 是否为手机号码
     */
    public static boolean isHmtMobile(CharSequence value) {
        if (value == null) {
            return false;
        }
        return HMT_MOBILE.matcher(value).matches();
    }

    /**
     * 获取手机号的类型
     *
     * @param value
     * @return
     */
    public static PhoneType getPhoneType(CharSequence value) {
        if (value == null) {
            return PhoneType.UNKNOW;
        }
        if (HMT_MOBILE.matcher(value).matches()) {
            return PhoneType.HMT;
        }
        return PhoneType.CN;
    }

    /**
     * 验证是否为身份证号
     *
     * @param value 值
     * @return 是否为身份证号
     */
    public static boolean isIDCard(CharSequence value) {
        if (value == null) {
            return false;
        }
        return ID_CARD.matcher(value).matches();
    }

    /**
     * 判断字符串是否是ip
     *
     * @return 是否是IP号
     */
    public static boolean isIp(CharSequence value) {
        if (value == null) {
            return false;
        }
        return IP.matcher(value).matches();
    }

    /**
     * 判断字符串是否是车牌号
     *
     * @return 是否是IP号
     */
    public static boolean isPlate(CharSequence value) {
        if (value == null) {
            return false;
        }
        return PLATE.matcher(value).matches();
    }

    /**
     * 判断rtmp
     *
     * @param playUrl
     * @return
     */
    public static boolean isRtmp(String playUrl) {
        return RTMP.matcher(playUrl).matches();
    }

    /**
     * 判断hls
     *
     * @param playUrl
     * @return
     */
    public static boolean isHls(String playUrl) {
        return HLS.matcher(playUrl).matches();
    }

    /**
     * 判断MP4
     *
     * @param playUrl
     * @return
     */
    public static boolean isMp4(String playUrl) {
        return MP4.matcher(playUrl).matches();
    }

    public static boolean isCron(String value) {
        if (value == null) {
            return false;
        }
        return CRON.matcher(value).matches();
    }

    /**
     * 是否是网址
     * @param netUrl 网址
     * @return 是否网址
     */
    public static boolean isNetUrl(String netUrl) {
        if (StringUtils.isBlank(netUrl)) {
            return false;
        }
        return NET_URL.matcher(netUrl).matches();
    }

    public static void main(String[] args) {
        // System.out.println(isInteger("100"));
        // System.out.println(isHls("https://cmgw-vpc.lechange.com:8890/LCO/6F07019PAZDBD18/0/1/20210122T093222/a30e7110ef555ceab8a0e43f30e11488.m3u8?proto=https"));
        // System.out.println(RegexUtil.isMobile("0085362400088"));
        // System.out.println(RegexUtil.isHmtMobile("0085362400088"));
        //
        // System.out.println(RegexUtil.isMobile("+85362400088"));
        // System.out.println(RegexUtil.isHmtMobile("+85362400088"));
        //
        // System.out.println(RegexUtil.isMobile("85362400088"));
        // System.out.println(RegexUtil.isHmtMobile("85362400088"));
        //
        // System.out.println(RegexUtil.isMobile("62400088"));
        // System.out.println(RegexUtil.isHmtMobile("62400088"));
        //
        // System.out.println("******************************");
        //
        // System.out.println(RegexUtil.isMobile("18682262103"));
        // System.out.println(RegexUtil.isHmtMobile("18682262103"));
        //
        // System.out.println(RegexUtil.isMobile("19182262103"));
        // System.out.println(RegexUtil.isHmtMobile("19182262103"));
        //
        // System.out.println(RegexUtil.isMobile("+8619182262103"));
        // System.out.println(RegexUtil.isHmtMobile("+8619182262103"));
        //
        // System.out.println(RegexUtil.isMobile("8619182262103"));
        // System.out.println(RegexUtil.isHmtMobile("8619182262103"));
        //
        // System.out.println("******************************");
        //
        // System.out.println(RegexUtil.getPhoneType("0085362400088"));
        // System.out.println(RegexUtil.getPhoneType("+85362400088"));
        // System.out.println(RegexUtil.getPhoneType("85362400088"));
        // System.out.println(RegexUtil.getPhoneType("62400088"));
        //
        // System.out.println("******************************");
        //
        // System.out.println(RegexUtil.getPhoneType("18682262103"));
        // System.out.println(RegexUtil.getPhoneType("19182262103"));
        // System.out.println(RegexUtil.getPhoneType("+8619182262103"));
        // System.out.println(RegexUtil.getPhoneType("8619182262103"));

        // System.out.println(RegexUtil.isCron("0 0/30 9-17 * * ?"));
        System.out.println(RegexUtil.isNetUrl("https://cms.whfciot.com"));
    }
}
